// imageUtils.js
export function getBase64Sync() {
    return new Promise((resolve, reject) => {
        let image = new Image();
        image.crossOrigin = "Anonymous";
        image.src = '/image.png';
        image.onload = function () {
            let canvas = document.createElement("canvas");
            canvas.width = this.naturalWidth; // 或者 'image.width' 如果不需要支持IE
            canvas.height = this.naturalHeight; // 或者 'image.height' 如果不需要支持IE
            canvas.getContext("2d").drawImage(this, 0, 0);
            const dataURL = canvas.toDataURL("image/png");
            resolve(dataURL);
        };
        image.onerror = reject;
    });
}

export function base64DataURLToArrayBuffer(dataURL) {
    const base64Regex = /^data:image\/(png|jpg|jpeg);base64,/;
    if (!base64Regex.test(dataURL)) {
        throw new Error("Unsupported data URL format");
    }
    const stringBase64 = dataURL.replace(base64Regex, "");
    const binaryString = window.atob(stringBase64);
    const len = binaryString.length;
    const bytes = new Uint8Array(len);
    for (let i = 0; i < len; i++) {
        bytes[i] = binaryString.charCodeAt(i);
    }
    return bytes.buffer;
}
